<?php
	if(!defined("ROOT_PATH")) exit("die Access ");
	class shopControl extends skymvc{
		
		public function __construct(){
			parent::__construct();
		}
		
		public function onDefault(){
			$where=" siteid=".SITEID." ";
			$url=APPADMIN."?m=shop&a=default";
			$order=" shopid DESC";
			$limit=20;
			$start=get("per_page","i");
			$orderby=get('orderby','i');
			$catid=get_post('catid','i');
			if($catid){
				$cids=M("category")->id_family($catid);
				$url.="&catid=$catid";
				if($cids){
					$where.=" AND catid in("._implode($cids).")";
				}else{
					$where.=" AND 1=2 ";
				}
				
			}
			$status=get('status','i');
			if($status){
				$where.=" AND status=$status";
				$url.="&status=".$status;
			}
			if($orderby){
				switch($orderby){
					case 1:
							$order="rank DESC";
						break;
					case 2:
							$order="sold_num DESC";
						break;
					case 3:
							$order="goodsgrade DESC";
						break;	
				}
				 
				$url.="&orderby=".urlencode($orderby);
			}
			
			$shopname=get('shopname','h');
			if($shopname){
				$where.=" AND shopname like '%".$shopname."%' ";
				$url.="&shopname=".urlencode($shopname);
			}
			
			$nickname=get('nickname','h');
			if($nickname){
				$where.=" AND nickname='".$nickname."' ";
				$url.="&nickname=".urlencode($nickname);
			}
			 
			$option=array(
				"start"=>intval(get_post('per_page')),
				"limit"=>$limit,
				"order"=>$order,
				"where"=>$where
			);
			$rscount=true;
			$data=M("shop")->select($option,$rscount);
			if($data){
			foreach($data as $k=>$v){
				$data[$k]['cname']=M("category")->selectOne(array("where"=>array("catid"=>$v['catid']),"fields"=>"cname"));
				if($v['catid_2nd']){
					$data[$k]['cname_2nd']=M("category")->selectOne(array("where"=>array("catid"=>$v['catid_2nd']),"fields"=>"cname"));
				}
				
				if($v['catid_3nd']){
					$data[$k]['cname_3nd']=M("category")->selectOne(array("where"=>array("catid"=>$v['catid_3nd']),"fields"=>"cname"));
				}
				
				
			}
		}
		$cat_list=M("category")->children(0,MODEL_SHOP_ID);
			$pagelist=$this->pagelist($rscount,$limit,$url);
			$cat_list=M("category")->children(0,MODEL_SHOP_ID);
			$this->smarty->assign(
				array(
					"data"=>$data,
					"pagelist"=>$pagelist,
					"rscount"=>$rscount,
					"url"=>$url,
					 "cat_list"=>$cat_list,
					 "citylist"=>M('district')->children(DISTRICTID),
				)
			);
			$this->smarty->display("shop/index.html");
		}
		
		public function onShow(){
			$shopid=get_post("shopid","i");
			if($shopid){
				$data=M("shop")->selectRow(array("where"=>"shopid={$shopid}"));
				
			}
			$cat_list=M("category")->children(0,MODEL_SHOP_ID);
			$this->smarty->assign(array(
				"data"=>$data,
				"is_invite_list"=>M("shop")->is_invite(),
			));
			$this->smarty->display("shop/show.html");
		}
		public function onAdd(){
			$shopid=get_post("shopid","i");
			if($shopid){
				$data=M("shop")->selectRow(array("where"=>"shopid={$shopid}"));
				$data['content']=M("shop_data")->selectOne(array("where"=>"shopid=".$shopid,"fields"=>" content"));
				$data['u_nickname']=M("user")->selectOne(array("where"=>" userid=".$data['userid'],"fields"=>"nickname"));
				 
			}
			$cat_list=M("category")->children(0,MODEL_SHOP_ID);
			$this->smarty->assign(array(
				"data"=>$data,
				 "cat_list"=>$cat_list,
				 "citylist"=>M('district')->children(DISTRICTID),
				 
			));
			$this->smarty->display("shop/add.html");
		}
		
		public function onSave(){
			
			$shopid=get_post("shopid","i");
			$data=M('shop')->postData();
			
			
			//gps信息
			if(post('latlng')){
				$latlng=explode(",",post('latlng'));
				$data['lat']=$latlng[0];
				$data['lng']=$latlng[1];
			}
			
			if($shopid){
			
				M("shop")->update($data,"shopid='$shopid'");
				M("shop_data")->update(array("content"=>post('content','x')),"shopid=".$shopid);
				if(!M("shop_data")->selectRow("shopid=".$shopid)){
					M("shop_data")->insert(array("shopid"=>$shopid,"content"=>post('content','x'),"dateline"=>time()),"shopid=".$shopid);
				}
			}else{
				$data['siteid']=SITEID;
				$data["dateline"]=time();
				$shopid=M("shop")->insert($data);
				M("shop_data")->insert(array("shopid"=>$shopid,"content"=>post('content','x'),"dateline"=>time()),"shopid=".$shopid);
			}
			$this->goall("保存成功");
		}
		
		public function onStatus(){
			$shopid=get_post('shopid',"i");
			$status=get_post("status","i");
			M("shop")->update(array("status"=>$status),"shopid=$shopid");
			exit(json_encode(array("error"=>0,"message"=>"状态修改成功")));
		}
		
		public function onRecommend(){
			$shopid=get_post('shopid',"i");
			$isrecommend=get_post("isrecommend","i");
			M("shop")->update(array("isrecommend"=>$isrecommend),"shopid=$shopid");
			exit(json_encode(array("error"=>0,"message"=>"推荐修改成功")));
		}
		
		
		public function onnew(){
			$shopid=get_post('shopid',"i");
			$isnew=get('isnew','i');
			M("shop")->update(array("isnew"=>$isnew),array("shopid"=>$shopid));	
			echo json_encode(array("error"=>0,"message"=>$this->lang['save_success']));	 
		}
		
		public function onhot(){
			$shopid=get_post('shopid',"i");
			$ishot=get('ishot','i');
			M("shop")->update(array("ishot"=>$ishot),array("shopid"=>$shopid));	
			echo json_encode(array("error"=>0,"message"=>$this->lang['save_success']));	 
		}
		
		public function onDelete(){
			$shopid=get_post('shopid',"i");
			M("shop")->delete("shopid={$shopid}");
			exit(json_encode(array("error"=>0,"message"=>"删除成功")));
		}
		
		public function onSearch(){
			$shopname=get_post('shopname','h');
			$data=M("shop")->select(array("where"=>" shopname like '%".$shopname."%'"));
			exit(json_encode($data));
		}
		
 
		
		
	}

?>